package behaviourNavigatore;

import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import utility.Auto;
import utility.MyDefaultWeightedEdge;
import utility.Triple;
import navigatore.AgenteNavigatore;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.OneShotBehaviour;

public class PutInTheStreet extends OneShotBehaviour{


	private static final long serialVersionUID = 2488912775525129836L;


	private AgenteNavigatore navigatore;

	@Override
	public void action() {

//		System.out.println("ACTION PUT IN THE STREET!!!");

		navigatore = (AgenteNavigatore)myAgent;

		List<Auto> listCars = navigatore.getWaitingCarsWithWays();

		Set<Triple<Dimension, String, String>> insieme = new HashSet<Triple<Dimension, String, String>>();

		int cont = listCars.size(), i=0;

		while(!listCars.isEmpty() && cont > 0) {

			Auto auto = listCars.get(i);

			if(!insieme.contains(auto.getStart())) {
				
				insieme.add(auto.getStart());

				for (MyDefaultWeightedEdge edge : navigatore.getSubGraph().edgesOf(auto.getStart())) {

					if(edge.equals(auto.getWay().get(0))) {
						
						Auto car;
						
						if(!auto.getWay().get(0).getListIdAuto().isEmpty()){
							car = navigatore.getAuto(auto.getWay().get(0).getListIdAuto().getLast());
							
							if(car.getKm() > 1) {
								navigatore.addAuto(auto);
								listCars.remove(i);
								edge.getListIdAuto().addLast(auto.getId());
							}
						}

						else {
							navigatore.addAuto(auto);
							listCars.remove(i);
							edge.getListIdAuto().addLast(auto.getId());
						}
						break;
					}
				}
			}

			else
				i++;

			cont--;

		}
	}
}

